const _function = require("../../utils/functionData.js")
Page({
  data: {
    isTeacher: true,
    formData: {
      type: 1, // 1教师 0学生 
      name: '',
      gender: '',
      avatar: '',
      title: '',
      majorType: '',
      majorCategory: '',
      address: '',
      phone: '',
      password: ''
    },
    jobTitle:[],
    majorTypes: [
      // '理科', '工科', '文科', '艺术类'
    ],
    majorCategories: {
      // '理科': ['数学', '物理', '化学'],
      // '工科': ['计算机', '机械', '电子'],
      // '文科': ['中文', '历史', '哲学'],
      // '艺术类': ['美术', '音乐', '舞蹈']
    }
  },
  onLoad(options) {
    this.getJobTitle()
    this.getJobType()
    this.getJobCategory()
  },
  // 获取学生预约列表
  getJobTitle(e) {
    console.log('选择时间', getApp().globalData.userInfo);
    const param = {
      // teacherId: getApp().globalData.userInfo.userId
    }
    _function.getJobTitle(param, this.getJobTitleBack, this)
  },
  getJobTitleBack(res) {
    let dataAll = res.data
    let that = this
    if (dataAll.code == 200) {
      that.setData({
        jobTitle:  dataAll.rows
      })
    }
  },
  // 获取学生预约列表
  getJobType(e) {
    console.log('选择时间', getApp().globalData.userInfo);
    const param = {
      // teacherId: getApp().globalData.userInfo.userId
    }
    _function.getJobType(param, this.getJobTypeBack, this)
  },
  getJobTypeBack(res) {
    let dataAll = res.data
    let that = this
    if (dataAll.code == 200) {
      that.setData({
        majorTypes: dataAll.rows
      })
    }
  },

  getJobCategory(e) {
    console.log('选择时间', getApp().globalData.userInfo);
    const param = {
      // teacherId: getApp().globalData.userInfo.userId
    }
    _function.getJobCategory(param, this.getJobCategoryBack, this)
  },
  getJobCategoryBack(res) {
    let dataAll = res.data
    let that = this
    if (dataAll.code == 200) {
      that.setData({
        majorCategories: dataAll.rows
      })
    }
  },
  // 用户类型切换 
  onTypeChange(e) {
    const type = parseInt(e.detail.value)
    this.setData({
      isTeacher: type === 1,
      'formData.type': type
    })
  },

  // 选择头像 
  chooseAvatar() {
    wx.chooseMedia({
      count: 1,
      mediaType: ['image'],
      success: (res) => {
        this.setData({
          'formData.avatar': res.tempFiles[0]().tempFilePath
        })
      }
    })
  },

  // 表单字段更新 
  onFieldChange(e) {
    const field = e.currentTarget.dataset.field
    const value = e.detail.value
    this.setData({
      [`formData.${field}`]: value
    })
  },

  // 地址选择 
  onAddressChange(e) {
    this.setData({
      'formData.address': e.detail.value.join('  ')
    })
  },

  // 表单提交 
  formSubmit(e) {
    const formData = e.detail.value
    // 添加表单验证逻辑 
    console.log(' 提交数据：', formData)
  }
})